﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;

namespace BYFW.Model.Authorization
{
    /// <summary>
    /// 权限管理系统中的角色类
    /// </summary>
    public class Role : BOL.BaseConfigEntity
    {
        public Role()
        {
            m_tableName = "auth_role";
            m_tblCacheManager.IsEnable = true;
            m_hasCode = true;
            m_hasComment = true;
            m_hasName = true;
        }

        protected bool m_isFix = false;

        /// <summary>
        /// 获取本角色是否是系统固有的，固有角色一般应用场合：具有明确的业务作用，在数据库中事先定义好，并在代码中硬编码
        /// </summary>
        public bool IsFix
        {
            get { return m_isFix; }
        }

        protected List<int> m_userIDs = null;
        /// <summary>
        /// 本用户的所有角色
        /// </summary>
        public List<int> UserIDs
        {
            get
            {
                if (m_userIDs == null)
                {
                    m_userIDs = this.GetUserIDs(m_id);
                }
                return m_userIDs;
            }
        }

        protected override void FillContainer()
        {
            base.FillContainer();
            this.m_container.Add("isFix", m_isFix);
        }

        public override bool GetProperties(DataRow record)
        {
            if (!base.GetProperties(record))
            {
                return false;
            }
            m_isFix = this.GetBoolValue(record, "isFix");
            return true;
        }
        public override int Delete(string transID)
        {
            if (!m_isFix)
            {
                return base.Delete(transID);
            }
            return 0;
        }
        public override int Update(string transID)
        {
            if (!m_isFix)
            {
                return base.Update(transID);
            }
            return 0;
        }

        /// <summary>
        /// 获取指定角色包含的所有用户
        /// </summary>
        /// <param name="roleID">角色ID</param>
        /// <returns></returns>
        public List<int> GetUserIDs(int roleID)
        {
            List<int> res = new List<int>();
            List<RURelation> urs = new RURelation().GetListByRID(roleID);
            for (int i = 0; i < urs.Count; i++)
            {
                bool find = false;
                for (int j = 0; j < res.Count; j++)
                {
                    if (urs[i].UserID == res[j])
                    {
                        find = true;
                        break;
                    }
                }
                if (!find)
                {
                    res.Add(urs[i].UserID);
                }
            }
            return res;
        }

    }
}
